package com.ivo.web.form.parser;

import java.util.List;

import com.ivo.model.DataType;
import com.ivo.model.Template;
import com.ivo.model.TemplateAttribute;
import com.ivo.util.StringUtil;
import com.ivo.web.form.def.Cell;
import com.ivo.web.form.def.CellLabel;
import com.ivo.web.form.def.CheckBox;
import com.ivo.web.form.def.CheckBoxGroup;
import com.ivo.web.form.def.DateField;
import com.ivo.web.form.def.Form;
import com.ivo.web.form.def.NumbericField;
import com.ivo.web.form.def.RadioBox;
import com.ivo.web.form.def.RadioGroup;
import com.ivo.web.form.def.SubTitle;
import com.ivo.web.form.def.Table;
import com.ivo.web.form.def.TableLayout;
import com.ivo.web.form.def.TextArea;
import com.ivo.web.form.def.TextField;

public class FormEncoder
{
	private static final String FORM_LAYOUT_TABLE = "TABLELAYOUT";
	protected Form form;
	protected Template template;
	protected List<TemplateAttribute> attributes;

	public FormEncoder(List<TemplateAttribute> attributes, Template template)
	{
		this.attributes = attributes;
		this.template = template;
	}

	public Form encode()
	{
		this.form = new Form();
		// ------------------------------------------------------------------
		encodeForm( form );
		// ------------------------------------------------------------------

		return this.form;
	}

	public void encodeForm(Form form)
	{
		form.setId( "basic-form" );
		form.setName( "basic-form" );
		// ------------------------------------------------------------------
		if( template.getDisplayType().getId().equals( FORM_LAYOUT_TABLE ) ) {
			TableLayout layout = new TableLayout();
			layout.setCols( template.getCols() );
			// ------------------------------------------------------------------
			form.setLayout( layout );

		} else {
			;
		}
		//--------------------------------------------------------------------------
		int tableIndex = 0;
		//--------------------------------------------------------------------------
		Table table = null;
		for( int i = 0; i < attributes.size(); i++ ) {
			TemplateAttribute attr = attributes.get( i );
			DataType dataType = attr.getDataType();
			if( attr.getTableIndex() <= 0 ) {
				encodeField( form, attr, dataType );
				//--------------------------------------------------------------------------
				tableIndex = 0;
			} else if( attr.getTableIndex() > 0 ) {
				if( tableIndex == 0 ) {
					table = new Table();
					table.setTableIndex( attr.getTableIndex() );
					form.getFields().add( table );
					form.getItems().add( attr.getTableIndex() + "", table );
				}
				tableIndex = attr.getTableIndex();
				//--------------------------------------------------------------------------
				encodeTableCell( table, attr, dataType );

			} // if

		} // for

	}

	public void encodeField(Form form, TemplateAttribute attr, DataType type)
	{
		if( type == null ) return;
		//---------------------------------------------------------------------
		if( type.getTypeId().equals( "DT009" ) ) {
			TextField field = new TextField();
			//---------------------------------------------------------------------
			field.setId( attr.getAttributeName() );
			field.setName( attr.getAttributeName() );
			field.setLabel( attr.getAttributeLabelText() );
			field.setValue( attr.getDefaultValue() );
			if( attr.getLength() > 0 ) {
				field.setMaxLength( attr.getLength() );
			}
			//---------------------------------------------------------------------
			form.getFields().add( field );
		} else if( type.getTypeId().equals( "DT010" ) ) {
			TextArea field = new TextArea();
			//---------------------------------------------------------------------
			field.setId( attr.getAttributeName() );
			field.setName( attr.getAttributeName() );
			field.setLabel( attr.getAttributeLabelText() );
			field.setValue( attr.getDefaultValue() );
			if( attr.getLength() > 0 ) {
				field.setMaxLength( attr.getLength() );
			}
			//---------------------------------------------------------------------
			form.getFields().add( field );
		} else if( type.getTypeId().equals( "DT011" ) ) {
			NumbericField field = new NumbericField();
			//---------------------------------------------------------------------
			field.setId( attr.getAttributeName() );
			field.setName( attr.getAttributeName() );
			field.setLabel( attr.getAttributeLabelText() );
			field.setValue( attr.getDefaultValue() );
			//---------------------------------------------------------------------
			form.getFields().add( field );
		} else if( type.getTypeId().equals( "DT016" ) ) {
			DateField field = new DateField();
			//---------------------------------------------------------------------
			field.setId( attr.getAttributeName() );
			field.setName( attr.getAttributeName() );
			field.setLabel( attr.getAttributeLabelText() );
			field.setValue( attr.getDefaultValue() );
			field.setFormat( "yy-mm-dd" );
			//---------------------------------------------------------------------
			form.getFields().add( field );
		} else if( type.getTypeId().equals( "DT006" ) ) {
			String dataSource = attr.getDataSource();
			RadioGroup group = new RadioGroup();
			group.setLabel( attr.getAttributeLabelText() );
			//---------------------------------------------------------------------
			if( StringUtil.isEmpty( dataSource ) ) {

				RadioBox field = new RadioBox();
				field.setId( attr.getAttributeName() + "_true" );
				field.setName( attr.getAttributeName() );
				field.setLabel( "Yes" );
				field.setValue( "true" );
				group.getRadios().add( field );
				//---------------------------------------------------------------------
				field = new RadioBox();
				field.setId( attr.getAttributeName() + "_false" );
				field.setName( attr.getAttributeName() );
				field.setLabel( "No" );
				field.setValue( "false" );
				group.getRadios().add( field );

			}
			//---------------------------------------------------------------------
			form.getFields().add( group );
		} else if( type.getTypeId().equals( "DT007" ) ) {
			String dataSource = attr.getDataSource();
			CheckBoxGroup group = new CheckBoxGroup();
			group.setLabel( attr.getAttributeLabelText() );
			//---------------------------------------------------------------------
			if( StringUtil.isEmpty( dataSource ) ) {

				CheckBox field = new CheckBox();
				field.setId( attr.getAttributeName() + "_cb" );
				field.setName( attr.getAttributeName() );
				field.setLabel( "Yes" );
				group.getCheckboxs().add( field );

			}
			//---------------------------------------------------------------------
			form.getFields().add( group );
		} else if( type.getTypeId().equals( "DT014" ) ) {
			SubTitle title = new SubTitle();
			title.setId( attr.getAttributeName() );
			title.setTitle( attr.getAttributeLabelText() );
			//---------------------------------------------------------------------
			form.getFields().add( title );
		}

	}

	public void encodeTableCell(Table table, TemplateAttribute attr, DataType type)
	{
		if( type == null ) return;
		//---------------------------------------------------------------------
		Cell cell = new Cell();
		table.getCells().add( cell );
		//---------------------------------------------------------------------
		if( type.getTypeId().equals( "DT009" ) ) {
			TextField field = new TextField();
			//---------------------------------------------------------------------
			field.setId( attr.getAttributeName() );
			field.setName( attr.getAttributeName() );
			field.setValue( attr.getDefaultValue() );
			if( attr.getLength() > 0 ) {
				field.setMaxLength( attr.getLength() );
			}
			//-----------------------------------------------------------------------
			cell.setItem( field );
		} else if( type.getTypeId().equals( "DT011" ) ) {
			NumbericField field = new NumbericField();
			//---------------------------------------------------------------------
			field.setId( attr.getAttributeName() );
			field.setName( attr.getAttributeName() );
			field.setLabel( attr.getAttributeLabelText() );
			field.setValue( attr.getDefaultValue() );
			//---------------------------------------------------------------------
			cell.setItem( field );
		} else if( type.getTypeId().equals( "DT016" ) ) {
			DateField field = new DateField();
			//---------------------------------------------------------------------
			field.setId( attr.getAttributeName() );
			field.setName( attr.getAttributeName() );
			field.setLabel( attr.getAttributeLabelText() );
			field.setValue( attr.getDefaultValue() );
			field.setFormat( "yy-mm-dd" );
			//---------------------------------------------------------------------
			cell.setItem( field );
		} else if( type.getTypeId().equals( "DT006" ) ) {
			String dataSource = attr.getDataSource();
			RadioGroup group = new RadioGroup();
			//---------------------------------------------------------------------
			if( StringUtil.isEmpty( dataSource ) ) {

				RadioBox field = new RadioBox();
				field.setId( attr.getAttributeName() + "_true" );
				field.setName( attr.getAttributeName() );
				field.setLabel( "Yes" );
				field.setValue( "true" );
				group.getRadios().add( field );
				//---------------------------------------------------------------------
				field = new RadioBox();
				field.setId( attr.getAttributeName() + "_false" );
				field.setName( attr.getAttributeName() );
				field.setLabel( "No" );
				field.setValue( "false" );
				group.getRadios().add( field );

			}
			//---------------------------------------------------------------------
			cell.setItem( group );
		} else if( type.getTypeId().equals( "DT007" ) ) {
			String dataSource = attr.getDataSource();
			CheckBoxGroup group = new CheckBoxGroup();
			//---------------------------------------------------------------------
			if( StringUtil.isEmpty( dataSource ) ) {

				CheckBox field = new CheckBox();
				field.setId( attr.getAttributeName() + "_cb" );
				field.setName( attr.getAttributeName() );
				field.setLabel( "Yes" );
				group.getCheckboxs().add( field );

			}
			//---------------------------------------------------------------------
			cell.setItem( group );
		} else if( type.getTypeId().equals( "DT015" ) ) {
			CellLabel field = new CellLabel();
			//---------------------------------------------------------------------
			field.setId( attr.getAttributeName() );
			field.setName( attr.getAttributeName() );
			field.setLabel( attr.getAttributeLabelText() );
			//---------------------------------------------------------------------
			cell.setItem( field );
		}

	}
}
